/*
 * CorsoConnectionHolder.java
 *
 * Created on 06. Juni 2007, 12:11
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package sbc.corso;

import corso.lang.*;
import org.apache.log4j.*;

/**
 *
 * @author mustermark
 */
public class CorsoConnectionHolder {
    
    protected CorsoConnection con;
    protected String userName;
    private String password;
    private int strategy;
    private int aid;
    protected String site;
    private String domain;
    private int port;
    static Logger logger = Logger.getLogger(CorsoConnectionHolder.class);
    /**
     * Creates a new instance of CorsoConnectionHolder
     */
    public CorsoConnectionHolder() {
        this("corso"," ", CorsoStrategy.PR_DEEP_EAGER, 1, "localhost", "", 5006);
        
    }
    
    public CorsoConnectionHolder(String user, String pass, int strat, int _aid, String _site, String _domain, int _port) {
        userName = user;
        password = pass;
        strategy = strat;
        aid = _aid;
        site = _site;
        domain = _domain;
        port = _port;
        connect();
    
    }
    
    public void connect() {
        con = new CorsoConnection ();
        try {
            con.connect (userName, password, new CorsoStrategy(strategy), aid, "", site, domain, port); 
            logger.info("Corso connect ok");
        } catch (Exception e) {
            logger.error("Exception: " + e);
            e.printStackTrace ();
            System.exit (-3);
        }
        
    }
    
    public CorsoConnection getConnection() { return con; }
    
    public CorsoVarOid getDefaultNamedVarOid(String name) throws CorsoException {
        return con.getNamedVarOid (name, site, null, false, CorsoConnection.NO_TIMEOUT);
    }
    
    public CorsoConstOid getDefaultNamedConstOid(String name) throws CorsoException {
        return con.getNamedConstOid(name, site, null, false, CorsoConnection.NO_TIMEOUT);
    }
    
    public CorsoVarOid createDefaultNamedVarOid(String name) throws CorsoException {
        return con.createNamedVarOid (con.getCurrentStrategy(), name, null);
    }
    
    public CorsoConstOid createDefaultNamedConstOid(String name) throws CorsoException {
        return con.createNamedConstOid (con.getCurrentStrategy(), name, null);
    }
}
